package parser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import log.Logger;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class PushParser {

	public boolean parseXML(String xml) {
		System.out.println("****** xml"+xml);
		boolean suscripto = false;

		try {
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
			.parse(new ByteArrayInputStream(xml.getBytes("ISO-8859-1")));
			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("*");
			//			int ok = 0;
			//			int error =1;
			String node = new String();
			String element = new String();
			for (int i = 0; i < list.getLength(); i++) {
				if (list.item(i).hasChildNodes()){
					Node value = list.item(i).getChildNodes().item(0);
					node = list.item(i).getNodeName();
					element = value.getNodeValue();
					if (node.equals("Estado")){
						if (element.equals("0")){
							suscripto=true;
						} 
					}
				}
			}
			System.out.println("****** Suscripto: "+suscripto);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Logger.log("PUSH parser is done, suscripto="+suscripto);

		return suscripto;

	}
}
